/**
  * 预约看房表单
  * @author z 2017-11-16
  */
(function(document, window, undefined){
	 // 选择房间
	 $('.Room dd').click(function(){
		if(!$(this).hasClass('livedRoom')){
		    $(this).removeClass('selectedRoom');
		    $(this).addClass('selectedRoom').siblings().removeClass('selectedRoom');
		    var roomPriceNum = $(this).children('.roomPrice').text();
		    $('.PriceNum .price').text(roomPriceNum);
		}      
	}); 
	// 我要看房
        $('#signup-btn').click(function(){
	        	//var roomPrice = $('.Room dd .roomPrice')[0];
	        	var noRoom = $('.Room dd').children().hasClass('roomPrice');
	        	var noSelectRoom = $('.Room dd').hasClass('selectedRoom');
	        	if(!noRoom){
	        		show_msg('本房子房间已住满，看看其它的吧');
	        	}else if(!noSelectRoom){
	        		show_msg('请先选择房间');
	        	}else{
	        		$('.singup-alert-box').addClass('show');
	        	}
	});
          // 关闭预约看房
	$('#signup_close').click(function(){
		$('.singup-alert-box').removeClass('show');
	});
	// 预约日期插件
	$("#dtBox").DateTimePicker({
		dateFormat: "yyyy-MM-dd",
		shortDayNames: ["", "", "", "", "", "", ""],
		fullDayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
		shortMonthNames: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
		fullMonthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],		
		titleContentDate: "设置日期",
		setButtonContent: "确定",
		clearButtonContent: "取消"
	});
	// 提交表单
	$('#submit-btn').click(function(){
		//  当前时间
		var current_date = new Date().getTime();
		 //  选择的时间
		var select_date = new Date($('#date').val().split('-').join(',')).getTime();
		var regMobile = /^0?1(3|5|7|8)\d{9}$/;
		var title = $('#housetitle').text();
		var room = $('.Room dd.selectedRoom').find('.roomNum').text();
		var name = $('#name').val();
		var phonenumber = $('#tel').val();
		var sex    = $('input[name=sex]:checked').val();
		var owner    = $('input[name=owner]:checked').val();
		var houseid    = $('#houseid').html();
		var date = $('#date').val();
		var need = $('#textarea').val();
		if(name == "" || name == null) {
			show_msg("名字不能为空");
		}else if(phonenumber == "" || phonenumber == null) {
			show_msg("手机号码不能为空");
		}else if(!regMobile.test(phonenumber)) {
			show_msg('手机号格式错误');
		}else if(date == "" || date == null) {
			show_msg("请选择预约日期");
		}else if(select_date <= current_date){
			show_msg("请核实预约日期");
		}else{
			$.ajax({
				type: "POST", 
				dataType: "JSON", 
				url: "http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=yuyue", 
				data: {
					title: title,
					room : room,
					name : name,
					mobile : phonenumber,
					sex : sex,
					owner:owner,
					num : houseid,
					date : date,
					need : need
				},
				success: function (data){ 
					var status = data;
					console.log(status);
					if(status == '1'){
						show_msg("预约成功");
					}else{
						show_msg("写入数据失败");
					}
					setTimeout(function(){
						$('.singup-alert-box').removeClass('show');
					},3000);
				}
			});
		}
	});
	//显示提示信息
	function show_msg(msg){
		$('.jbPhoneTip').show().html(msg);
		setTimeout(function(){
			$('.jbPhoneTip').hide();
		},2000);
	}
})(document, window)